﻿//文件指定编译编码UTF-8
#pragma execution_character_set("utf-8")

/**
*    @file:    autorestarter.cpp
*    @brief:
*    @author:  sanshi522@163.com
*    @date:    2025-02-19  18:01
*/
#include "autorestarter.h"
#include <progressmanage.h>
#include <QApplication>
#include <QThread>
#include <string>
using namespace std;


AutoRestarter::AutoRestarter(int waitTime, const QString &programPath)
    : m_waitTime(waitTime), m_programPath(programPath){

}

void AutoRestarter::start()
{
    waitProgramQuit();
    restartProgram();
    std::exit(0);
}

void AutoRestarter::waitProgramQuit()
{
    while(true){
        string progressPath = m_programPath.toStdString();
        DWORD progressId;
        //如果查不到该进程则退出等待
        if(!ProgressManage::getProcessIdByPath(progressPath,progressId)){
            break;
        }
        QThread::msleep(100);
    }
}

void AutoRestarter::restartProgram()
{
    while(true){
        string progressPath = m_programPath.toStdString();
        ProgressManage::startProcessByPath_A(progressPath);
        QThread::msleep(5000);
        DWORD progressId;
        //如果查到该进程则退出。
        if(ProgressManage::getProcessIdByPath(progressPath,progressId)){
            break;
        }
        QThread::msleep(100);
    }
}
